package com.ruoyi.net.service.impl;

import java.util.Collection;
import java.util.List;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.net.domain.NetElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.net.mapper.SysAlarmInfoMapper;
import com.ruoyi.net.domain.SysAlarmInfo;
import com.ruoyi.net.service.ISysAlarmInfoService;

import javax.annotation.PostConstruct;

/**
 * 告警代码Service业务层处理
 * 
 * @author tony
 * @date 2025-02-16
 */
@Service
public class SysAlarmInfoServiceImpl implements ISysAlarmInfoService 
{
    @Autowired
    private SysAlarmInfoMapper sysAlarmInfoMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 项目启动时，初始化告警代码信息到缓存
     */
    @PostConstruct
    public void init()
    {
        loadingAlarmInfoCache();
    }

    @Override
    public void loadingAlarmInfoCache() {
        List<SysAlarmInfo> alarmInfos = sysAlarmInfoMapper.selectSysAlarmInfoList(new SysAlarmInfo());
        for (SysAlarmInfo alarmInfo : alarmInfos) {
            redisCache.setCacheObject(getCacheKey(alarmInfo.getAlarmCode().toString()), alarmInfo);
        }
    }

    /**
     * 重置缓存数据
     */
    @Override
    public void resetConfigCache()
    {
        clearAlarmInfoCache();
        loadingAlarmInfoCache();
    }


    /**
     * 清空缓存数据
     */
    @Override
    public void clearAlarmInfoCache() {
        Collection<String> keys = redisCache.keys(CacheConstants.ALARM_INFO_KEY + "*");
        redisCache.deleteObject(keys);
    }

    /**
     * 设置cache key
     *
     * @param key 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String key)
    {
        return CacheConstants.ALARM_INFO_KEY + key;
    }

    /**
     * 查询告警代码
     * 
     * @param id 告警代码主键
     * @return 告警代码
     */
    @Override
    public SysAlarmInfo selectSysAlarmInfoById(String id)
    {
        return sysAlarmInfoMapper.selectSysAlarmInfoById(id);
    }

    /**
     * 查询告警代码列表
     * 
     * @param sysAlarmInfo 告警代码
     * @return 告警代码
     */
    @Override
    public List<SysAlarmInfo> selectSysAlarmInfoList(SysAlarmInfo sysAlarmInfo)
    {
        return sysAlarmInfoMapper.selectSysAlarmInfoList(sysAlarmInfo);
    }

    /**
     * 新增告警代码
     * 
     * @param sysAlarmInfo 告警代码
     * @return 结果
     */
    @Override
    public int insertSysAlarmInfo(SysAlarmInfo sysAlarmInfo)
    {
        String uuid = IdUtils.simpleUUID();
        if (StringUtils.isEmpty(sysAlarmInfo.getId())) {
            sysAlarmInfo.setId(uuid);
        }
        return sysAlarmInfoMapper.insertSysAlarmInfo(sysAlarmInfo);
    }

    /**
     * 修改告警代码
     * 
     * @param sysAlarmInfo 告警代码
     * @return 结果
     */
    @Override
    public int updateSysAlarmInfo(SysAlarmInfo sysAlarmInfo)
    {
        return sysAlarmInfoMapper.updateSysAlarmInfo(sysAlarmInfo);
    }

    /**
     * 批量删除告警代码
     * 
     * @param ids 需要删除的告警代码主键
     * @return 结果
     */
    @Override
    public int deleteSysAlarmInfoByIds(String[] ids)
    {
        return sysAlarmInfoMapper.deleteSysAlarmInfoByIds(ids);
    }

    /**
     * 删除告警代码信息
     * 
     * @param id 告警代码主键
     * @return 结果
     */
    @Override
    public int deleteSysAlarmInfoById(String id)
    {
        return sysAlarmInfoMapper.deleteSysAlarmInfoById(id);
    }
}
